package com.bbx.taxi.client.UpdateChecker.baidu;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.baidu.autoupdatesdk.AppUpdateInfo;
import com.baidu.autoupdatesdk.AppUpdateInfoForInstall;
import com.baidu.autoupdatesdk.BDAutoUpdateSDK;
import com.baidu.autoupdatesdk.CPCheckUpdateCallback;
import com.bbx.api.sdk.model.passanger.Return.Version;
import com.bbx.taxi.client.UpdateChecker.ui.MyUpdateLoad.UpdateType;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickRightListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickSingleListener;
import com.bbx.taxi.official.R;
import com.example.loadview.LoadingDialog;

public class MyCPCheckUpdateCallback implements CPCheckUpdateCallback {
	private Activity context;
	private LoadingDialog loaddialog;
	/** 不更新类型，0为启动页不更新的话直接调整，1为设置检查更新，不更新则弹出提示 */
	private UpdateType noupdatetype;

	public interface OnBaiduUpdateListener {
		/**

		 * 百度开始执行更新或者取消更新

		 */
		public void onBaiduUpdate();
	};

	public OnBaiduUpdateListener mOnBaiduUpdateListener = null;

	/**

	 * 检查版本更新,且监听

	 */
	public void setOnBaiduUpdateListener(OnBaiduUpdateListener l) {
		mOnBaiduUpdateListener = l;
	}

	public MyCPCheckUpdateCallback(Activity context, LoadingDialog loaddialog,
			UpdateType noupdatetype) {
		this.context = context;
		this.loaddialog = loaddialog;
		this.noupdatetype = noupdatetype;
		if (loaddialog != null && !loaddialog.isShowing()) {
			loaddialog.show();
		}
	}

	@Override
	public void onCheckUpdateCallback(final AppUpdateInfo info,
			final AppUpdateInfoForInstall infoForInstall) {
		if (info != null) {
			String change = info.getAppChangeLog();
			// 强制更新


			if (change != null
					&& change.contains(context
							.getString(R.string.forced_update))) {
				onShowForcedUpdate(change, info);
			} else {
				onShowNoForcedUpdate(change, info);
			}

		} else if (infoForInstall != null
				&& !TextUtils.isEmpty(infoForInstall.getInstallPath())) {
			String change = infoForInstall.getAppChangeLog();
			// 强制更新


			if (change != null
					&& change.contains(context
							.getString(R.string.forced_update))) {
				onShowForcedUpdate(change, infoForInstall);
			} else {
				onShowNoForcedUpdate(change, infoForInstall);
			}
		} else {
			if (noupdatetype == UpdateType.TYPE_QIDONG) {
				if (mOnBaiduUpdateListener != null)
					mOnBaiduUpdateListener.onBaiduUpdate();
			} else if (noupdatetype == UpdateType.TYPE_SET) {
				final MyAlertDailog dialog = new MyAlertDailog(context);
				dialog.setTitle(context.getString(R.string.mydailog_update_latest));
				dialog.setContent2("");
				dialog.setSingle(context.getString(R.string.mydailog_confirm));
				dialog.show();
				dialog.setOnClickSingleListener(new onClickSingleListener() {
					@Override
					public void onClickSingle() {
						dialog.dismiss();
						if (mOnBaiduUpdateListener != null)
							mOnBaiduUpdateListener.onBaiduUpdate();
					}
				});
			}
		}
		if (loaddialog != null)
			loaddialog.dismiss();
	}

	/**

	 * 显示强制更新

	 */
	public void onShowForcedUpdate(String content, final AppUpdateInfo info) {
		final MyAlertDailog dialog = new MyAlertDailog(context);
		dialog.setTitle(context.getResources().getString(
				R.string.mydailog_update_content));
		dialog.setContent2(!content.equals("") ? content : "");
		dialog.setSingle(context.getResources().getString(
				R.string.mydailog_confirm_update));
		dialog.show();
		dialog.setOnClickSingleListener(new onClickSingleListener() {
			@Override
			public void onClickSingle() {
				dialog.dismiss();
				BDAutoUpdateSDK.cpUpdateDownload(context, info,
						new UpdateDownloadCallback(context, true));
			}
		});
	}

	/**

	 * 显示强制更新

	 */
	public void onShowForcedUpdate(String content,
			final AppUpdateInfoForInstall infoForInstall) {
		final MyAlertDailog dialog = new MyAlertDailog(context);
		dialog.setTitle(context.getResources().getString(
				R.string.mydailog_update_content));
		dialog.setContent2(!content.equals("") ? content :"");
		dialog.setSingle(context.getResources().getString(
				R.string.mydailog_confirm_update));
		dialog.show();
		dialog.setOnClickSingleListener(new onClickSingleListener() {
			@Override
			public void onClickSingle() {
				dialog.dismiss();
				BDAutoUpdateSDK.cpUpdateInstall(context,
						infoForInstall.getInstallPath());
			}
		});
	}

	/**

	 * 显示不强制更新

	 */
	public void onShowNoForcedUpdate(String content, final AppUpdateInfo info) {
		final MyAlertDailog dialog = new MyAlertDailog(context);
		dialog.setTitle(context.getResources().getString(
				R.string.mydailog_update_content));
		dialog.setContent2(!content.equals("") ? content :"");
		dialog.setLeftButtonText(context.getResources().getString(
				R.string.mydailog_cancel_update));
		dialog.setRightButtonText(context.getResources().getString(
				R.string.mydailog_confirm_update));
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
		dialog.setOnClickLeftListener(new onClickLeftListener() {
			@Override
			public void onClickLeft() {
				dialog.dismiss();
				if (mOnBaiduUpdateListener != null)
					mOnBaiduUpdateListener.onBaiduUpdate();
			}
		});
		dialog.setOnClickRightListener(new onClickRightListener() {

			@Override
			public void onClickRight() {
				dialog.dismiss();
				BDAutoUpdateSDK.cpUpdateDownload(context, info,
						new UpdateDownloadCallback(context, false));
				if (mOnBaiduUpdateListener != null)
					mOnBaiduUpdateListener.onBaiduUpdate();
			}
		});
	}

	/**

	 * 显示不强制更新

	 */
	public void onShowNoForcedUpdate(String content,
			final AppUpdateInfoForInstall infoForInstall) {
		final MyAlertDailog dialog = new MyAlertDailog(context);
		dialog.setTitle(context.getResources().getString(
				R.string.mydailog_update_content));
		dialog.setContent2(!content.equals("") ? content : "");
		dialog.setLeftButtonText(context.getResources().getString(
				R.string.mydailog_cancel_update));
		dialog.setRightButtonText(context.getResources().getString(
				R.string.mydailog_confirm_update));
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
		dialog.setOnClickLeftListener(new onClickLeftListener() {
			@Override
			public void onClickLeft() {
				dialog.dismiss();
				if (mOnBaiduUpdateListener != null)
					mOnBaiduUpdateListener.onBaiduUpdate();
			}
		});
		dialog.setOnClickRightListener(new onClickRightListener() {

			@Override
			public void onClickRight() {
				dialog.dismiss();
				BDAutoUpdateSDK.cpUpdateInstall(context,
						infoForInstall.getInstallPath());
				if (mOnBaiduUpdateListener != null)
					mOnBaiduUpdateListener.onBaiduUpdate();
			}
		});
	}
}